package com.lzq.model.singleton.reflex;

import java.lang.reflect.Constructor;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/14 10:54
 * @updateUser : modify
 * @updateTime : 2022/11/14 10:54
 * @updateRemark :
 */
public class Singleton {
    private Singleton() {
        if (!flag) {
            throw new RuntimeException("对象已创建");
        } else {
            flag = true;
        }
    }

    private static boolean flag = false;

    private static class SingletonWithin {
        private static final Singleton SINGLETON = new Singleton();
    }
}

class ReflexTest {
    public static void main(String[] args) throws Exception {
        Class<Singleton> singletonClass = Singleton.class;
        Constructor declaredConstructors = singletonClass.getDeclaredConstructor();
        declaredConstructors.setAccessible(true);
        Singleton instance = (Singleton) declaredConstructors.newInstance();
        Singleton instance1 = (Singleton) declaredConstructors.newInstance();
        System.out.println(instance == instance1);
    }
}
